package com.hdax.dm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hdax.dm.beans.base.image;
import com.hdax.dm.beans.item.Item;
import com.hdax.dm.beans.item.cinema;
import com.hdax.dm.dto.descDto.getItemsDto;
import com.hdax.dm.dto.descDto.getRecommendDto;
import com.hdax.dm.feign.imagefeign;
import com.hdax.dm.mappers.desc.getItemsMapper;
import com.hdax.dm.mapstucts.descMapper.getItemsDtoMapper;
import com.hdax.dm.mapstucts.descMapper.getRecommendDtoMapper;
import com.hdax.dm.service.getItemsService;
import com.hdax.dm.utils.response.CommonResponse;
import com.hdax.dm.utils.response.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class getItemsServiceImpl extends ServiceImpl<getItemsMapper, Item> implements getItemsService {

    @Autowired
    private getItemsDtoMapper getItemsDtoMapper;

    @Autowired
    private getRecommendDtoMapper getRecommendDtoMapper;

    @Autowired
    private imagefeign imagefeign;

    @Autowired
    private cinemaServiceImpl cinemaService;

    @Override
    public CommonResponse<getItemsDto> getItems(Map<String,Integer> params) {
        Integer id = params.get("id");
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id",id);
        Item item = baseMapper.selectOne(queryWrapper);
        getItemsDto dto = getItemsDtoMapper.toTarget(item);

        image img = imagefeign.image(dto.getId(), 2L ,1L);
        String imgUrl =img == null ? "":img.getImgUrl();
        dto.setImgUrl(imgUrl);

        cinema c = cinemaService.selectOne(dto.getCinemaId().intValue());

        dto.setAdress(c.getAddress());
        dto.setAreaId(c.getAreaId());
        dto.setAreaName(c.getAreaName());

        return ResponseUtil.returnSuccess(dto);
    }

    @Override
    public CommonResponse<List<getRecommendDto>> getRecommend(Map<String, Integer> params) {
        Integer itemtypeId = params.get("itemTypeId");
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("itemtype2Id",itemtypeId);
        queryWrapper.eq("isRecommend",1);
        queryWrapper.last("limit 6");
        List<Item> lists = baseMapper.selectList(queryWrapper);
        List<getRecommendDto> dtos = getRecommendDtoMapper.toTargetList(lists);

        dtos.forEach(dto ->{
            image img = imagefeign.image(dto.getId(),2L,1L);
            String imgUrl =img == null ? "":img.getImgUrl();
            dto.setImgUrl(imgUrl);
        });

        return ResponseUtil.returnSuccess(dtos);
    }
}
